/**
 * 
 */
package de.nak.roommgmt.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;

import de.nak.roommgmt.model.Room;
import de.nak.roommgmt.service.RoomService;

/**
 * Action class for creating a new room.
 * @author Stephan Anft
 */
public class AddRoomAction extends ActionSupport {
	
	/** {@inheritDoc} */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RoomForm roomForm = (RoomForm) form;
		// Get the new room
		Room room = roomForm.getRoom();
		// Save room
		RoomService roomService = (RoomService)getWebApplicationContext().getBean("roomService");
		roomService.createRoom(room.getBuilding(), room.getRoomNumber(), room.getSeats(), room.isBeamer());
		// Reload list
		roomForm.setRooms(roomService.listRooms());
		roomForm.setSelectedKey(null);
		return mapping.findForward("success");
	}

}
